package com.kyle.pagingdemo.ui.adapter

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.navigation.NavController
import androidx.navigation.fragment.FragmentNavigatorExtras
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestListener
import com.kyle.pagingdemo.R
import com.kyle.pagingdemo.data.model.Post
import com.kyle.pagingdemo.databinding.ItemArticleBinding

/**
 * author: liliang
 * date: 1/10/21 1:50 AM
 * desc:
 */
class PostAdapter(private val selectionHandler: (View, Int) -> Unit) :
    PagingDataAdapter<Post, PostAdapter.PostViewHolder>(REPO_COMPARATOR) {

    companion object {
        private val REPO_COMPARATOR = object : DiffUtil.ItemCallback<Post>() {
            override fun areItemsTheSame(oldItem: Post, newItem: Post): Boolean =
                oldItem.id == newItem.id

            override fun areContentsTheSame(oldItem: Post, newItem: Post): Boolean =
                oldItem == newItem
        }
    }

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
        holder.bind(position, getItem(position))
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
        val binding = ItemArticleBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return PostViewHolder(binding, selectionHandler)
    }

    class PostViewHolder(
        private val binding: ItemArticleBinding,
        private val selectionHandler: (View, Int) -> Unit
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(position: Int, post: Post?) {
            val context = itemView.context
            binding.itemContainer.transitionName =
                context.getString(R.string.container_card_item, position)
            binding.itemContainer.setOnClickListener { card ->
                selectionHandler(card, position)
            }
            with(binding.imageView) {
                post?.let {
                    updateLayoutParams<ConstraintLayout.LayoutParams> {
                        dimensionRatio = "${it.width}:${it.height}"
                    }
                    val filename = "${it.md5}${it.ext}"
                    val imageUrl =
                        "https://images.anime-pictures.net/${it.md5.subSequence(0, 3)}/$filename"
                    loadPhotoUrlWithThumbnail(imageUrl, it.big_preview, it.color)
                }

            }
        }
    }
}

private const val CROSS_FADE_DURATION = 350

fun ImageView.loadPhotoUrlWithThumbnail(
    url: String,
    thumbnailUrl: String,
    color: List<Int>?,
    requestListener: RequestListener<Drawable>? = null
) {
    color?.let { background = ColorDrawable(Color.rgb(color[0], color[1], color[2])) }
    Glide.with(this.context)
        .load(url)
        .thumbnail(
            Glide.with(this.context).load(thumbnailUrl)
        )
        .transition(DrawableTransitionOptions.withCrossFade(CROSS_FADE_DURATION))
        .addListener(requestListener)
        .into(this)
        .clearOnDetach()
}